#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

struct MContext
{
    GLFWwindow *window;
    unsigned int shader_program;
    unsigned int vao;
    unsigned int vbo;
};

void init_gl_environ(MContext *context);
void prepare_datas(MContext *context);
void setup_shader_program(MContext *context);
void run(MContext *context);
void free_context(MContext *context);

void processInput(GLFWwindow *window);
void framebuffer_size_cb(GLFWwindow *window, int w, int h);

const int SCR_WIDTH = 800;
const int SCR_HEIGHT = 600;
const char *vertexShaderSource = "#version 330 core\n"
                                 "layout (location = 0) in vec3 aPos;\n"
                                 "void main()\n"
                                 "{\n"
                                 "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
                                 "}\0";
const char *fragmentShaderSource = "#version 330 core\n"
                                   "out vec4 FragColor;\n"
                                   "void main()\n"
                                   "{\n"
                                   "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
                                   "}\n\0";

int main()
{
    MContext context;
    init_gl_environ(&context);
    if (context.window == NULL)
        return -1;

    prepare_datas(&context);
    run(&context);
    free_context(&context);
    glfwTerminate();
    return 0;
}

void init_gl_environ(MContext *context)
{
    // glfw: initialize and configure
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    context->window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Hello Triangle", NULL, NULL);
    if (context->window == NULL)
    {
        std::cout << "Failed to create GLFW Window" << std::endl;
        glfwTerminate();
        return;
    }

    glfwMakeContextCurrent(context->window);
    glfwSetFramebufferSizeCallback(context->window, framebuffer_size_cb);

    // load glad
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Falied to initialize GLAD" << std::endl;
        glfwTerminate();
        return;
    }
}

void prepare_datas(MContext *context)
{
    setup_shader_program(context);
    float triangle[] = {
        -0.5, -0.5, 0,
        0.5, -0.5, 0,
        0, 0.5, 0};
    glGenVertexArrays(1, &context->vao);
    glGenBuffers(1, &context->vbo);
    // data
    glBindVertexArray(context->vao);
    glBindBuffer(GL_ARRAY_BUFFER, context->vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);
    glBindVertexArray(0);
}
void setup_shader_program(MContext *context)
{
    unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    unsigned int frameShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    glShaderSource(frameShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(frameShader);
    // link
    context->shader_program = glCreateProgram();
    glAttachShader(context->shader_program, vertexShader);
    glAttachShader(context->shader_program, frameShader);
    glLinkProgram(context->shader_program);
}

void run(MContext *context)
{
    while (!glfwWindowShouldClose(context->window))
    {
        // input
        // -----
        processInput(context->window);

        //render
        // ----
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // draw triangle
        glUseProgram(context->shader_program);
        glBindVertexArray(context->vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(context->window);
        glfwPollEvents();
    }
}

void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

void framebuffer_size_cb(GLFWwindow *window, int w, int h)
{
    glViewport(0, 0, w, h);
}

void free_context(MContext *context)
{
    glDeleteVertexArrays(1, &context->vao);
    glDeleteBuffers(1, &context->vbo);
    glDeleteProgram(context->shader_program);
}